package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NAsphaltMatchColdBo;
import com.ruoyi.zhgd.domain.vo.NAsphaltMatchColdVo;
import com.ruoyi.zhgd.domain.NAsphaltMatchCold;
import com.ruoyi.zhgd.mapper.NAsphaltMatchColdMapper;
import com.ruoyi.zhgd.service.INAsphaltMatchColdService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青混合料冷料配比Service业务层处理
 *
 * @author tll
 * @date 2024-01-18
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NAsphaltMatchColdServiceImpl implements INAsphaltMatchColdService {

    private final NAsphaltMatchColdMapper baseMapper;

    /**
     * 查询沥青混合料冷料配比
     */
    @Override
    public NAsphaltMatchColdVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询沥青混合料冷料配比列表
     */
    @Override
    public TableDataInfo<NAsphaltMatchColdVo> queryPageList(NAsphaltMatchColdBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NAsphaltMatchCold> lqw = buildQueryWrapper(bo);
        Page<NAsphaltMatchColdVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青混合料冷料配比列表
     */
    @Override
    public List<NAsphaltMatchColdVo> queryList(NAsphaltMatchColdBo bo) {
        LambdaQueryWrapper<NAsphaltMatchCold> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NAsphaltMatchCold> buildQueryWrapper(NAsphaltMatchColdBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NAsphaltMatchCold> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getGravel1() != null, NAsphaltMatchCold::getGravel1, bo.getGravel1());
        lqw.eq(bo.getGravel2() != null, NAsphaltMatchCold::getGravel2, bo.getGravel2());
        lqw.eq(bo.getGravel3() != null, NAsphaltMatchCold::getGravel3, bo.getGravel3());
        lqw.eq(bo.getGravel4() != null, NAsphaltMatchCold::getGravel4, bo.getGravel4());
        lqw.eq(bo.getGravel5() != null, NAsphaltMatchCold::getGravel5, bo.getGravel5());
        lqw.eq(bo.getGravel6() != null, NAsphaltMatchCold::getGravel6, bo.getGravel6());
        lqw.eq(bo.getPowder1() != null, NAsphaltMatchCold::getPowder1, bo.getPowder1());
        lqw.eq(bo.getPowder2() != null, NAsphaltMatchCold::getPowder2, bo.getPowder2());
        lqw.eq(bo.getPowder3() != null, NAsphaltMatchCold::getPowder3, bo.getPowder3());
        lqw.eq(bo.getRecycledMaterial() != null, NAsphaltMatchCold::getRecycledMaterial, bo.getRecycledMaterial());
        lqw.eq(bo.getAsphalt() != null, NAsphaltMatchCold::getAsphalt, bo.getAsphalt());
        lqw.eq(bo.getWoodFiber() != null, NAsphaltMatchCold::getWoodFiber, bo.getWoodFiber());
        lqw.like(StringUtils.isNotBlank(bo.getGravel1Name()), NAsphaltMatchCold::getGravel1Name, bo.getGravel1Name());
        lqw.like(StringUtils.isNotBlank(bo.getGravel2Name()), NAsphaltMatchCold::getGravel2Name, bo.getGravel2Name());
        lqw.like(StringUtils.isNotBlank(bo.getGravel3Name()), NAsphaltMatchCold::getGravel3Name, bo.getGravel3Name());
        lqw.like(StringUtils.isNotBlank(bo.getGravel4Name()), NAsphaltMatchCold::getGravel4Name, bo.getGravel4Name());
        lqw.like(StringUtils.isNotBlank(bo.getGravel5Name()), NAsphaltMatchCold::getGravel5Name, bo.getGravel5Name());
        lqw.like(StringUtils.isNotBlank(bo.getGravel6Name()), NAsphaltMatchCold::getGravel6Name, bo.getGravel6Name());
        lqw.like(StringUtils.isNotBlank(bo.getPowder1Name()), NAsphaltMatchCold::getPowder1Name, bo.getPowder1Name());
        lqw.like(StringUtils.isNotBlank(bo.getPowder2Name()), NAsphaltMatchCold::getPowder2Name, bo.getPowder2Name());
        lqw.like(StringUtils.isNotBlank(bo.getPowder3Name()), NAsphaltMatchCold::getPowder3Name, bo.getPowder3Name());
        lqw.eq(bo.getCreator() != null, NAsphaltMatchCold::getCreator, bo.getCreator());
        lqw.eq(bo.getCreateDate() != null, NAsphaltMatchCold::getCreateDate, bo.getCreateDate());
        lqw.eq(bo.getUpdater() != null, NAsphaltMatchCold::getUpdater, bo.getUpdater());
        lqw.eq(bo.getUpdateDate() != null, NAsphaltMatchCold::getUpdateDate, bo.getUpdateDate());
        return lqw;
    }

    /**
     * 新增沥青混合料冷料配比
     */
    @Override
    public Boolean insertByBo(NAsphaltMatchColdBo bo) {
        NAsphaltMatchCold add = BeanUtil.toBean(bo, NAsphaltMatchCold.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改沥青混合料冷料配比
     */
    @Override
    public Boolean updateByBo(NAsphaltMatchColdBo bo) {
        NAsphaltMatchCold update = BeanUtil.toBean(bo, NAsphaltMatchCold.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NAsphaltMatchCold entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青混合料冷料配比
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 添加配比冷料配比
     *
     * @param entity
     */
    @Override
    public void save(NAsphaltMatchCold entity) {
        baseMapper.insertOrUpdate(entity);
    }

    /**
     * 获取生产配比信息
     *
     * @param id
     * @return
     */
    @Override
    public NAsphaltMatchCold find(String id) {
        return baseMapper.selectById(id);
    }

}
